package dbPhase.hypeerweb;

//import static org.junit.Assert.*;
import junit.framework.TestCase;

import org.junit.Test;

public class HyPeerWebTest extends TestCase {

	@Test
	public void testAddNode() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
		Node node5 = new Node(5);
		Node node7 = new Node(7);

		hypeerweb.addNode(node5);
		assertTrue(hypeerweb.contains(node5));

		hypeerweb.addNode(node7);
		assertTrue(hypeerweb.contains(node7));

		hypeerweb.addNode(node5);
		assertTrue(hypeerweb.contains(node5));
		assertTrue(hypeerweb.contains(node7));

		hypeerweb.addNode(null);
		assertTrue(hypeerweb.contains(node5));
		assertTrue(hypeerweb.contains(node7));
	}

	@Test
	public void testClear() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();

		hypeerweb.addNode(new Node(7));
		hypeerweb.addNode(new Node(6));
		hypeerweb.addNode(new Node(5));

		hypeerweb.clear();
		assertTrue(hypeerweb.size() == 0);
	}

	@Test
	public void testContains() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
		Node node1 = new Node(1);
		Node node2 = new Node(2);
		Node node3 = new Node(3);

		hypeerweb.addNode(node1);
		assertTrue(hypeerweb.contains(node1));
		assertTrue(!hypeerweb.contains(node2));

		hypeerweb.addNode(node2);
		assertTrue(hypeerweb.contains(node1));
		assertTrue(hypeerweb.contains(node2));
		assertTrue(!hypeerweb.contains(node3));

		hypeerweb.addNode(node3);
		assertTrue(hypeerweb.contains(node1));
		assertTrue(hypeerweb.contains(node2));
		assertTrue(hypeerweb.contains(node3));
	}

	@Test
	public void testGetHyperwebDataBase() {
		fail("Not yet implemented");
	}

	@Test
	public void testGetNode() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
		Node node3 = new Node(3);

		hypeerweb.addNode(node3);
		assertEquals(3, hypeerweb.getNode(0).getWebId().getValue());
	}

	@Test
	public void testGetSingleton() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
		assertTrue(hypeerweb != null);
	}

	@Test
	public void testReload() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
		Node node3 = new Node(3);

		hypeerweb.addNode(node3);
		hypeerweb.saveToDatabase();
		hypeerweb.clear();
		HyPeerWeb hypeerweb2 = HyPeerWeb.getSingleton();
		hypeerweb2.reload();
		assertTrue(hypeerweb2.size() == 1);
		assertTrue(hypeerweb.contains(node3));
		assertTrue(hypeerweb2 != null);
	}

	@Test
	public void testReloadDBName() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
		Node node3 = new Node(3);

		hypeerweb.addNode(node3);
		hypeerweb.saveToDatabase();
		hypeerweb.clear();
		hypeerweb.reload("HyPeerWeb.db");
		assertTrue(hypeerweb != null);
		assertTrue(hypeerweb.size() == 1);
	}

	@Test
	public void testRemoveNode() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
		Node node5 = new Node(5);
		Node node7 = new Node(7);

		hypeerweb.addNode(node5);
		assertTrue(hypeerweb.contains(node5));

		hypeerweb.addNode(node7);
		assertTrue(hypeerweb.contains(node7));
		assertTrue(hypeerweb.size() == 2);

		hypeerweb.removeNode(node5);
		assertFalse(hypeerweb.contains(node5));
		assertTrue(hypeerweb.size() == 1);
	}

	@Test
	public void testSaveToDatabase() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
		try {
			hypeerweb.saveToDatabase();
		}
		catch (Exception e) {
			fail();
		}
	}

	@Test
	public void testSize() {
		HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
		hypeerweb.clear();
		Node node10 = new Node(10);
		Node node11 = new Node(11);
		Node node12 = new Node(12);
		Node node13 = new Node(13);
		Node node14 = new Node(14);

		hypeerweb.addNode(node10);
		assertTrue(hypeerweb.size() == 1);
		hypeerweb.addNode(node11);
		assertTrue(hypeerweb.size() == 2);
		hypeerweb.addNode(node12);
		assertTrue(hypeerweb.size() == 3);
		hypeerweb.addNode(node13);
		assertTrue(hypeerweb.size() == 4);
		hypeerweb.addNode(node14);
		assertTrue(hypeerweb.size() == 5);
	}

}
